//Author: Rahul Nayak

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.Random;

public class Board extends JPanel implements ActionListener
{
	public ArrayList<Location> locations;
    public Board() 
    {
    	locations = new ArrayList<Location>();
    	setPreferredSize(new Dimension (684, 559));
		GridLayout layout = new GridLayout(8, 8, 0, 0);
		setLayout (layout);
		setUpBoard();
		for(int i = 0; i<64; i++)
		{
			add(locations.get(i).getBox());	
			locations.get(i).getBox().addActionListener(this);
		}
    }    
   	
   	public void setUpBoard()
   	{
   		int rowVal = 0;
   		for(int i = 0; i<8; i++)
   		{	
   			for(int a = 0; a<8; a++)
   			{
   				if(rowVal%2 == 0)
   				{
   					if(a%2 == 0)
   						locations.add(new Location(Color.YELLOW,a,i,null));
   					else
   						locations.add(new Location(new Color(205,133,63),a,i,null));
   				}
   				else
   				{
					if(a%2 == 0)
   						locations.add(new Location(new Color(205,133,63),a,i,null));
   					else
   						locations.add(new Location(Color.YELLOW,a,i,null));   					
   				}
   			}
   			rowVal++;
   		}
   	}
   	
   	public void addPieces()
   	{
   		locations.get(0).setPiece(new GRook());
   		locations.get(7).setPiece(new GRook());
   		locations.get(2).setPiece(new GBishop());
   		locations.get(5).setPiece(new GBishop());
   		locations.get(1).setPiece(new GKnight());
   		locations.get(6).setPiece(new GKnight());
   		locations.get(3).setPiece(new GQueen());
   		locations.get(4).setPiece(new GKing());
		locations.get(8).setPiece(new GPawn());
   		locations.get(9).setPiece(new GPawn());
   		locations.get(10).setPiece(new GPawn());
   		locations.get(11).setPiece(new GPawn());   		
   		locations.get(12).setPiece(new GPawn());
   		locations.get(13).setPiece(new GPawn());
   		locations.get(14).setPiece(new GPawn());
   		locations.get(15).setPiece(new GPawn());
   		
   		locations.get(56).setPiece(new BRook());
   		locations.get(63).setPiece(new BRook());
   		locations.get(58).setPiece(new BBishop());
   		locations.get(61).setPiece(new BBishop());
   		locations.get(57).setPiece(new BKnight());
   		locations.get(62).setPiece(new BKnight());
   		locations.get(59).setPiece(new BQueen());
   		locations.get(60).setPiece(new BKing());
		locations.get(48).setPiece(new BPawn());
   		locations.get(49).setPiece(new BPawn());
   		locations.get(50).setPiece(new BPawn());
   		locations.get(51).setPiece(new BPawn());   		
   		locations.get(52).setPiece(new BPawn());
   		locations.get(53).setPiece(new BPawn());
   		locations.get(54).setPiece(new BPawn());
   		locations.get(55).setPiece(new BPawn());
   		
   	}
   	
   	public void actionPerformed(ActionEvent e)
   	{
   		Random r = new Random();
   		int i = r.nextInt(64);
   		locations.get(i).setPiece(null);	
   
   	}
   	
   	public Board getBoard()
   	{
   		return this;
   	}
   	
   	public void updateBoard()
   	{
   		removeAll();
   		for(int i = 0; i<64; i++)
			add(locations.get(i).getBox());
   	}
   	
   	
   	public static void main(String[] args)
   	{
   		Board b = new Board();
   		b.addPieces();
   		JFrame frame = new JFrame ("JPanel Preview");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(b);
		frame.pack();
		frame.setVisible (true);
   	}
}